<?php
/**
 * @category   Epic
 * @package    Epic_Loader
 * @author     Epic Core Team <rukanhitokiri@gmail.com>
 */
class Epic_Loader_Autoloader
{
	/**
     * Singleton instance
     * @var Epic_Loader_Autoloader
     */
    private static $_instance;
   
    /**
     * Singleton instance
     *
     * @return Epic_Loader_Autoloader
     */
    public static function getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    } 
   
    /**
     * Enforce singleton; disallow cloning
     *
     * @return void
     */
	private function __clone() {}

	/**
     * Constructor
     *
     * Instantiate using {@link getInstance()}; Epic_Loader_Autoloader is a singleton
     * object.
     *
     * Constructor set the parameters necessary for the autoload
     *
     * @return void
     */
    private function __construct()
    {
    	// Remove the existing autoload
		spl_autoload_register(null, false);
		// Specify extensions can be loaded
		spl_autoload_extensions('.php');
		spl_autoload_register(array(__CLASS__,'_loader'));
    }
   
	/**
     * Implement autoload
     *
	 * @return void
     */
	private function _loader($class)
	{
		$path = str_replace('_', '/', $class);
		$file = $path . ".php";
		include $file;
		if (!class_exists($class, false) && !interface_exists($class, false)) {
			require 'Epic/Loader/Exception.php';
			throw new Epic_Loader_Exception('The requested class or interface: ' . $class . ' was not found.');
		}
	}
}